var Match = function(paramLeagueId) {
	var leagueId = paramLeagueId;
	var contentContainerId = "content";
	var $contentContainer = $("#" + contentContainerId);
	var sefl = this;
	
	//label
	var timeLabel = "Thời gian";
	var matchLabel = "Trận đấu";
	var oddsLabel = "Cá trận";
	var infoLabel = "Euro 2012 tại Ucraina và Poland";
	
	this.init = function() {
		$.ajax({
		    url: "match/getListByLeague",
			type: "POST",
			data: {"league_id":leagueId},
			dataType: "json",
			async: true,
			success: [initSuccess],
			beforeSend: beforeInit
		});		
	};	
	
	var initSuccess = function(data, textStatus, jqXHR) {
		if (textStatus == "success") {
			displayMatchs(data);
		}
	};
		
	var beforeInit = function(jqXHR, settings) {
			
	};
	
	var displayMatchs = function(data) {
		var $matchListContainer = createMathListContainer();
		var $matchListHeader = createMatchListHeader();
		var $matchOddsListHeader = createMatchOddsListHeader(data.odds_list);
		
		$matchListContainer.append($matchListHeader);
		$matchListContainer.append($matchOddsListHeader);
		
		for (var i = 0; i < data.match_data_list.length; i++) {
			var $matchElement = createMatchElement(data.match_data_list[i], data.odds_list);
			$matchListContainer.append($matchElement);
		}
		
		$contentContainer.append($matchListContainer);
	}
	
	var createMatchListHeader = function() {
		var $matchHeader = $(document.createElement("div"));
		$matchHeader.addClass("match_list_header");
		
		var $matchHeaderTime = $(document.createElement("div"));
		$matchHeaderTime.addClass("time");
		$matchHeaderTime.addClass("element");
		$matchHeaderTime.html(timeLabel);
		
		var $matchHeaderMatch = $(document.createElement("div"));
		$matchHeaderMatch.addClass("match");
		$matchHeaderMatch.addClass("element");
		$matchHeaderMatch.html(matchLabel);
		
		var $matchHeaderOdds = $(document.createElement("div"));
		$matchHeaderOdds.addClass("odds");
		$matchHeaderOdds.addClass("element");
		$matchHeaderOdds.html(oddsLabel);
		
		$matchHeader.append($matchHeaderTime);
		$matchHeader.append($matchHeaderMatch);
		$matchHeader.append($matchHeaderOdds);
		
		return $matchHeader;
	}
	
	var createMatchOddsListHeader = function(oddsList) {
		var $matchOddsHeader = $(document.createElement("div"));
		$matchOddsHeader.addClass("match_odds_header");
		
		var $matchOddsHeaderInfo = $(document.createElement("div"));
		$matchOddsHeaderInfo.addClass("info");
		$matchOddsHeaderInfo.addClass("element");
		$matchOddsHeaderInfo.html(infoLabel);
		
		$matchOddsHeader.append($matchOddsHeaderInfo);
		
		for (var i = 0; i < oddsList.length; i++) {
			var $matchHeaderOdds = $(document.createElement("div"));
			$matchHeaderOdds.addClass("odds");
			$matchHeaderOdds.addClass("element");
			$matchHeaderOdds.html(oddsList[i].name);
			$matchOddsHeader.append($matchHeaderOdds);
		}
		
		
		
		return $matchOddsHeader;
	}
	
	var createMathListContainer = function() {
		var $matchListContainer = $(document.createElement("div"));
		$matchListContainer.addClass("match_list_container");
		
		return $matchListContainer;
	}
	
	var createMatchElement = function(matchData, oddsList) {
		var $matchElementContainer = $(document.createElement("div"));		
		$matchElementContainer.addClass("match_element_container");
					
		var $matchElementTime = $(document.createElement("div"));
		$matchElementTime.addClass("time");
		$matchElementTime.addClass("element");
		$matchElementTime.html(matchData.match.start_time_day);
		$matchElementContainer.append($matchElementTime);
		
		var $matchElementTeamOdds = $(document.createElement("div"));
		$matchElementTeamOdds.addClass("match_team_odds");
		$matchElementTeamOdds.addClass("element");
		$matchElementContainer.append($matchElementTeamOdds);
		
		//render oddses for team 1
		var $matchElementTeamOddsItem = $(document.createElement("div"));
		$matchElementTeamOddsItem.addClass("match_team_odds_item");
		$matchElementTeamOddsItem.addClass("element");
		$matchElementTeamOdds.append($matchElementTeamOddsItem);
		
		var $matchElementTeam1 = $(document.createElement("div"));
		$matchElementTeam1.addClass("match_team_item");
		$matchElementTeam1.addClass("element");
		$matchElementTeam1.html(matchData.match.league_team_id1);
		$matchElementTeamOddsItem.append($matchElementTeam1);
		
		for(var i = 0; i < oddsList.length; i++) {
			var odds = oddsList[i];
			var isFoundMatchOdds = false;
			var matchOdds = null;
			for (var j = 0; j < matchData.match_odds_list.length; j++) {
				var match_odds = matchData.match_odds_list[j];
				if (odds.odds_id == match_odds.odds_id && match_odds.league_team_id == matchData.match.league_team_id1) {
					isFoundMatchOdds = true;
					matchOdds = match_odds;
					break;
				}
			}
			if (isFoundMatchOdds) {
				var $matchElementOdds = $(document.createElement("div"));
				$matchElementOdds.addClass("match_odds_item");
				$matchElementOdds.addClass("element");
				$matchElementOdds.html(matchOdds.ratio);
				$matchElementTeamOddsItem.append($matchElementOdds);
			}
		}
		
		//render oddses for team 1
		var $matchElementTeamOddsItem = $(document.createElement("div"));
		$matchElementTeamOddsItem.addClass("match_team_odds_item");
		$matchElementTeamOddsItem.addClass("element");
		$matchElementTeamOdds.append($matchElementTeamOddsItem);
		
		var $matchElementTeam2 = $(document.createElement("div"));
		$matchElementTeam2.addClass("match_team_item");
		$matchElementTeam2.addClass("element");
		$matchElementTeam2.html(matchData.match.league_team_id2);
		$matchElementTeamOddsItem.append($matchElementTeam2);
		
		for(var i = 0; i < oddsList.length; i++) {
			var odds = oddsList[i];
			var isFoundMatchOdds = false;
			var matchOdds = null;
			for (var j = 0; j < matchData.match_odds_list.length; j++) {
				var match_odds = matchData.match_odds_list[j];
				if (odds.odds_id == match_odds.odds_id && match_odds.league_team_id == matchData.match.league_team_id2) {
					isFoundMatchOdds = true;
					matchOdds = match_odds;
					break;
				}
			}
			if (isFoundMatchOdds) {
				var $matchElementOdds = $(document.createElement("div"));
				$matchElementOdds.addClass("match_odds_item");
				$matchElementOdds.addClass("element");
				$matchElementOdds.html(matchOdds.ratio);
				$matchElementTeamOddsItem.append($matchElementOdds);
			}
		}
		
		return $matchElementContainer;
	}
	
	var leagueMenuElementClick = function(event) {
		alert('clcik');
	}	
	
	sefl.init();
}